package com.stone.back.dish.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.stone.back.dish.entity.BfCombo;
import com.stone.back.dish.entity.BfComboDish;
import com.stone.base.dao.BaseDao;
import com.stone.base.entity.Page;

/**
 * 套餐管理
 * @author Stone P
 *
 */
@Repository
public class ComboDao extends BaseDao implements IComboDao {
	/**
	 * 查询套餐分页信息
	 * @param pageNumber	页数
	 * @param comboChName	中文套餐名
	 * @param comboEnName	英文套餐名
	 * @param priceMin		单价
	 * @param priceMax		单价
	 * @param startDate		创建日期
	 * @param endDate		创建日期
	 * @return
	 */
	@Override
	public Page selectComboPage(int pageNumber, String comboChName, String comboEnName, String priceMin, String priceMax, String startDate, String endDate) {
		StringBuilder hql = new StringBuilder("from BfCombo where status != 0 ");
		if(StringUtils.isNotBlank(comboChName)) {
			hql.append(" and combochname like '%" + comboChName + "%'");
		}
		if(StringUtils.isNotBlank(comboEnName)) {
			hql.append(" and comboenname like '%" + comboEnName + "%'");
		}
		if(StringUtils.isNotBlank(priceMax)) {
			hql.append(" and price <= " + priceMax);
		}
		if(StringUtils.isNotBlank(priceMin)) {
			hql.append(" and price >= " + priceMin);
		}
		if(StringUtils.isNotBlank(startDate)) {
			hql.append(" and createdate >= '" + startDate + "'");
		}
		if(StringUtils.isNotBlank(endDate)) {
			hql.append(" and createdate <= '" + endDate + "'");
		}
		hql.append(" order by comboid desc");
 		Page page = this.queryPage(hql.toString(), pageNumber);
		return page;
	}
	
	/**
	 * 查询套餐列表
	 * @return
	 */
	@Override
    public List<BfCombo> selectComboList() {
		String hql = "from BfCombo where status = 1";
		return this.hibernateTemplate.find(hql);
    }

	/**
	 * 增加套餐
	 * @param combo	套餐信息
	 * @return
	 */
	@Override
	public Integer addCombo(BfCombo combo) {
		Integer comboId = (Integer) this.hibernateTemplate.save(combo);
		return comboId;
	}

	/**
	 * 套餐和菜品关系
	 * @param comboDish	套餐菜品信息
	 * @return
	 */
	@Override
	public Integer addComboDish(BfComboDish comboDish) {
		Integer comboDishId = (Integer) this.hibernateTemplate.save(comboDish);
		return comboDishId; 
	}

	/**
	 * 修改套餐
	 * @param combo	套餐信息
	 * @return
	 */
	@Override
	public void updateCombo(BfCombo combo) {
		this.hibernateTemplate.update(combo);
	}

	/**
	 * 删除套餐和菜品关系
	 * @param comboId	套餐Id
	 * @return
	 */
	@Override
	public void deleteComboDish(int comboId) {
		String sql = "delete from bf_combo_dish where comboid = " + comboId;
		Session session = this.hibernateTemplate.getSessionFactory().openSession();
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
	}

	/**
	 * 查询套餐详情
	 * @param comboId	套餐Id
	 * @return
	 */
	@Override
	public BfCombo selectComboDetail(int comboId) {
		return this.hibernateTemplate.get(BfCombo.class, comboId);
	}

	/**
	 * 查询套餐和菜品关系详情
	 * @param comboId	套餐Id
	 * @return
	 */
	@Override
	public List<BfComboDish> selectComboDishDetail(int comboId) {
		List<BfComboDish> comboDishList = this.hibernateTemplate.find("from BfComboDish where comboid = ? order by dishid desc", comboId);
		return comboDishList;
	}
}
